package com.magnificent.atlantis.server.ui.panels.user.table;

import com.magnificent.atlantis.common.entity.ServerUser;
import com.magnificent.atlantis.server.AtlantisServerConstants;
import com.magnificent.atlantis.server.database.AtlantisDataSource;
import com.magnificent.atlantis.server.ui.panels.common.AbstractTableModel;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * User: Babanin
 * Date: 04.04.2009
 * Time: 12:26:11
 */
public class UserTableModel extends AbstractTableModel<ServerUser> {

    private List<ServerUser> data;

    public UserTableModel() {
        super(ServerUser.class);

        this.data = AtlantisDataSource.getInstance().getUsers();
    }

    @Override
    public int getRowCount() {
        if (data == null) {
            data = new ArrayList<ServerUser>();
        }

        return data.size();
    }

    @Override
    public int getColumnCount() {
        return AtlantisServerConstants.USER_TABLE_COLUMN_NAMES.length;
    }

    @Override
    public Object getValueAt(int row, int column) {
        ServerUser serverUser = data.get(row);

        switch (column) {
            case 0:
                return serverUser.getId();
            case 1:
                return serverUser.getLogin();
            case 2:
                return serverUser.getName();
            case 3:
                return serverUser.getSurname();
            case 4:
                Date date = serverUser.getLastUpdate();
                return date != null ? AtlantisServerConstants.USER_TABLE_DATE_FORMAT.format(date) : "never";
            case 5:
                return serverUser.isBaned() ? "yes" : "no";
        }

        return "";
    }

    public ServerUser getValue(int row) {
        return data.get(row);
    }

    protected void updateModel(List<ServerUser> data) {
        this.data = data;
        fireTableDataChanged();
    }

    @Override
    public String getColumnName(int column) {
        return AtlantisServerConstants.USER_TABLE_COLUMN_NAMES[column];
    }
}
